const http = require('../../../utils/http.js')
const util = require('../../../utils/util.js')
const Base64 = require('../../../utils/base64.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        showPoster: true,
        isCollect: true,
        initialTime: 0,
        startTime: '00:00',
        endTime: '00:00',
    },

    /**
     * 播放视频
     */
    playVideo() {
        let that = this,
            tv_url = that.data.tv_url
        wx.getNetworkType({
            success: res => {
                let is_allow = wx.getStorageSync('is_allow') || 1
                var networkType = res.networkType;
                let flag1 = (is_allow == 1 && networkType == 'wifi') || is_allow == 2 ? true : false;
                if (flag1 && tv_url) {
                    that.setData({
                        showPoster: false,
                        prograss: 0,
                        initialTime: 0,
                        startTime: '00:00',
                    })
                    that.videoContext.play()
                } else {
                    wx.showToast({
                        title: '您不处于WIFI状态,请链接wifi后使用',
                        icon: 'none',
                        duration: 500
                    })
                }
            }
        })
    },
    downVedio() {
        wx.showToast({
            title: '小程序无法下载视频',
            icon: 'none',
            duration: 1000
        })
    },
    /**
     * 获取视频时长和已播放节点
     */
    getVideoTime(e) {
        let currentTime = e.detail.currentTime,
            duration = e.detail.duration
        this.setData({
            prograss: (currentTime / duration) * 100,
            initialTime: currentTime,
            startTime: util.formatSeconds(currentTime),
            endTime: util.formatSeconds(duration)
        })
    },

    /**
     * 结束/暂停播放显示封面图
     */
    showPoster() {
        this.setData({
            showPoster: true
        })
    },

    /**
     * 返回
     */
    goBack() {
        http.goBack()
    },

    /**
     * 收藏
     */
    collect() {
        let that = this,
            isCollect = that.data.isCollect,
            url = 'Lesson_userStart',
            token = wx.getStorageSync('token'),
            id = that.data.lid
        if (isCollect) {
            http._post(
                url, { token: token, type: 1, id: id, del: 1 },
                res => {
                    let data = res.data
                    if (data.re === '1') {

                        wx.showToast({
                            title: '取消收藏成功',
                            icon: 'success',
                            image: '/images/pay_suc.png',
                            duration: 1000,
                            success: function() {
                                setTimeout(function() {
                                    that.getIntroduction()
                                }, 800)
                            }
                        })
                    } else {
                        wx.showToast({
                            title: data.info,
                            icon: 'none',
                            duration: 1000
                        })
                    }
                },
                err => {
                    console.log(err)
                }
            )
        } else {
            http._post(
                url, { token: token, type: 1, id: id },
                res => {
                    console.log(res.data)

                    let data = res.data
                    if (data.re === '1') {
                        wx.showToast({
                            title: '收藏成功',
                            icon: 'success',
                            image: '/images/pay_suc.png',
                            duration: 1000,
                            success: function() {
                                setTimeout(function() {
                                    that.getIntroduction()
                                }, 800)
                            }
                        })
                    } else {
                        wx.showToast({
                            title: data.info,
                            icon: 'none',
                            duration: 1000
                        })
                    }
                },
                err => {
                    console.log(err)
                }
            )
        }
    },

    /**
     * 选择课时播放
     */
    chooseKs(e) {
        let url = e.currentTarget.dataset.url,
            time = e.currentTarget.dataset.time,
            name = e.currentTarget.dataset.name
        this.setData({
            tv_url: Base64.CusBASE64.decoder(url).replace("UNO", "").replace("HACHA", ""),
            endTime: util.formatSeconds(time),
            name: name,
            showPoster: true,
            prograss: 0,
            initialTime: 0,
            startTime: '00:00',
        })
    },

    /**
     * 获取小节列表
     */
    getThreadtopList(lid, tid, nid) {
        let that = this,
            url = 'Lesson_formThreadtopList',
            token = wx.getStorageSync('token')

        http._post(
            url, { token: token, lid: lid, tid: tid, nid: nid },
            res => {
                // console.log(res.data)

                let data = res.data
                if (data.re === '1') {
                    let temp_arr = []
                    for (let item of data.data) {
                        let tv_url = Base64.CusBASE64.decoder(item.tv_url).replace("UNO", "").replace("HACHA", "")
                        temp_arr.push(tv_url)
                    }
                    that.setData({
                        tvArr: temp_arr,
                        ksList: data.data,
                        isCollect: data.data[0].is_collect
                    })
                } else {
                    that.setData({
                        tvArr: [],
                        ksList: []
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 获取视频播放链接
     */
    getVideourl() {
        let that = this,
            url = 'Lesson_getVideourl',
            token = wx.getStorageSync('token'),
            lid = that.data.lid

        http._post(
            url, { token: token, lid: lid },
            res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    let temp_url = Base64.CusBASE64.decoder(data.data.tv_url).replace("UNO", "").replace("HACHA", ""),
                        endTime = data.data.test_time

                    that.setData({
                        tv_url: temp_url,
                        endTime: util.formatSeconds(endTime)
                    })
                } else {
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 1000
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 获取课程简介
     */
    getIntroduction() {
        let that = this,
            token = wx.getStorageSync('token'),
            lid = that.data.lid,
            url = 'Lesson_formIntroduction'

        wx.showLoading({
            title: '加载中……',
            mask: true
        })

        http._post(
            url, { token: token, lid: lid },
            res => {
                // console.log(res.data)
                let data = res.data
                wx.hideLoading()
                if (data.re === '1') {
                    let material = data.data.materialList,
                        temp_arr = []

                    for (let item of material) {
                        temp_arr.push(item.lid)
                    }
                    temp_arr.join(',')
                    that.setData({
                        introduce: data.data,
                        isCollect: data.data.want_learn == 1 ? true : false,
                        starNumber: Number(data.data.star),
                        id_str: temp_arr
                    })
                } else {
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 1000
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.videoContext = wx.createVideoContext('course_video')
        let lid = options.lid,
            tid = options.tid,
            nid = options.nid,
            name = options.name
        // console.log(lid, tid, nid)
        this.getThreadtopList(lid, tid, nid)
        this.setData({
            name: name,
            lid: lid
        })
        this.getIntroduction() // 获取课程简介
        this.getVideourl() // 获取视频播放链接
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    }
})